/*
 * @Author: heinan
 * @Date: 2023-07-23 22:50:43
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-01 21:22:35
 */
"use strict";
const { Controller } = require("egg");

/**
 *  @Controller 视图权限
 */
class ViewController extends Controller {
  /**
   * @Summary 视图列表接口
   * @Description 根据view_authority_id接口查询视图列表
   * @Router GET /api/v1/view/{id}
   * @Request path string *id
   */
  async show() {
    const result = await this.ctx.service.view.show(this.ctx.params);
    if (result) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
        data: result,
      };
    }
  }
  /**
   * @Summary 视图列表接口
   * @Description 获取视图列表
   * @Router GET /api/v1/view
   */
  async index() {
    const result = await this.ctx.service.view.index();
    if (result.length) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
        data: [],
      };
    }
  }
  /**
   * @Summary 添加视图接口
   * @Description 添加视图接口
   * @Router POST /api/v1/view
   * @Request body createView *body
   */
  async create() {
    try {
      this.ctx.validate({
        view_authority_text: "string",
        view_name: "string",
      });
    } catch (err) {
      this.ctx.status = 400;
      return (this.ctx.body = {
        code: 0,
        error: err.errors,
      });
    }
    const result = await this.ctx.service.view.create(this.ctx.request.body);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "新增成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "新增失败!",
      };
    }
  }
  /**
   * @Summary 更新接口权限
   * @Description 根据身份 id, view_authority_text, view_name ,接口更新字段
   * @Router PUT /api/v1/view/{id}
   * @Request path string *id
   * @Request body updateView *body
   */
  async update() {
    try {
      this.ctx.validate({
        view_authority_text: "string",
        view_name: "string",
      });
    } catch (err) {
      this.ctx.status = 400;
      return (this.ctx.body = {
        code: 0,
        error: err.errors,
      });
    }
    const result = await this.ctx.service.view.update({
      ...this.ctx.params,
      ...this.ctx.request.body,
    });
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "修改成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败!",
      };
    }
  }
  /**
   * @Summary 删除视图接口
   * @Description 根据视图id删除接口
   * @Router DELETE /api/v1/view/{id}
   * @Request path string *id
   */
  async destroy() {
    const result = await this.ctx.service.view.destroy(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败!",
      };
    }
  }
}

module.exports = ViewController;
